﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using BaseTypesLib.Model.Backgrounds;

namespace BaseTypesLib.UiTypeEditors
{
    public partial class BackgroundControl : UserControl
    {

        public BackgroundControl(Background current)
        {
            InitializeComponent();
            sR.ValueChanged += solid_ValueChanged;
            sG.ValueChanged += solid_ValueChanged;
            sB.ValueChanged += solid_ValueChanged;

            tR.ValueChanged += transparent_ValueChanged;
            tG.ValueChanged += transparent_ValueChanged;
            tB.ValueChanged += transparent_ValueChanged;
            tA.ValueChanged += transparent_ValueChanged;

            c1R.ValueChanged += grad_ValueChanged;
            c1G.ValueChanged += grad_ValueChanged;
            c1B.ValueChanged += grad_ValueChanged;
            c2R.ValueChanged += grad_ValueChanged;
            c2G.ValueChanged += grad_ValueChanged;
            c2B.ValueChanged += grad_ValueChanged;

            radHor.CheckedChanged += grad_ValueChanged;
            //init the controls;
            InitControl(current);
        }

        private void grad_ValueChanged(object sender, EventArgs e)
        {
            Graphics g = gradientPanel.CreateGraphics();

            Color c1 = Color.FromArgb((byte)c1R.Value, (byte)c1G.Value, (byte)c1B.Value);
            Color c2 = Color.FromArgb((byte)c2R.Value, (byte)c2G.Value, (byte)c2B.Value);


            LinearGradientBrush brush = GetGradientBrush(false, c1, c2, gradientPanel.Width, gradientPanel.Height);
            if(radHor.Checked)
                brush = GetGradientBrush(true, c1, c2, gradientPanel.Width, gradientPanel.Height);

            g.FillRectangle(brush, new Rectangle(0, 0, gradientPanel.Width, gradientPanel.Height));
            g.Dispose();
        }
        private void solid_ValueChanged(object sender, EventArgs e)
        {
            solidPanel.BackColor = GetSolidColor();
        }
        private void transparent_ValueChanged(object sender, EventArgs e)
        {
            transparentPanel.BackColor = GetTransparentColor();
        }



        private void InitControl(Background current)
        {
            cbPosX.SelectedIndex = 0;
            cbPosY.SelectedIndex = 0;
            cbRepeat.SelectedIndex = 0;

            if (current == null)
            {
                tabControl.SelectedIndex = 0;
            }
            else if (current is LinearGradientBackground)
            {
                tabControl.SelectedIndex = 3;
                LinearGradientBackground bg = (LinearGradientBackground)current;
                c1R.Value = bg.Color1.R; c1G.Value = bg.Color1.G;
                c1B.Value = bg.Color1.B;
                c2R.Value = bg.Color2.R; c2G.Value = bg.Color2.G;
                c2B.Value = bg.Color2.B;
                if (bg.Orientation == GradientOrientation.Vertical)
                    radVert.Checked = true;
                else
                    radHor.Checked = true;
                grad_ValueChanged(null, null);
            }
            else if (current is TransparentColorBackground)
            {
                tabControl.SelectedIndex = 2;
                TransparentColorBackground bg = (TransparentColorBackground)current;
                tR.Value = bg.Color.R; tG.Value = bg.Color.G;
                tB.Value = bg.Color.B; tA.Value = bg.Alpha;
                transparent_ValueChanged(null, null);

            }
            else if (current is SolidColorBackground)
            {
                tabControl.SelectedIndex = 1;
                SolidColorBackground bg=(SolidColorBackground)current;
                sR.Value = bg.Color.R; sG.Value = bg.Color.G;
                sB.Value = bg.Color.B;
                solid_ValueChanged(null, null);
            }
            else
            {
                tabControl.SelectedIndex = 4;
                ImageBackground bg = (ImageBackground)current;
                txtPath.Text = bg.ImagePath;
                cbRepeat.SelectedIndex = Enum.GetNames(typeof(ImageRepeat)).ToList().IndexOf(bg.Repeat.ToString());
                cbPosX.SelectedIndex = Enum.GetNames(typeof(PositionX)).ToList().IndexOf(bg.PositionX.ToString());
                cbPosY.SelectedIndex = Enum.GetNames(typeof(PositionY)).ToList().IndexOf(bg.PositionY.ToString());
            }
        }

        private Color GetSolidColor()
        {
            return Color.FromArgb((int)sR.Value, (int)sG.Value, (int)sB.Value);
        }
        private Color GetTransparentColor()
        {
            return Color.FromArgb((int)tA.Value, (int)tR.Value, (int)tG.Value, (int)tB.Value);
        }
        private LinearGradientBrush GetGradientBrush(bool horiz, Color c1, Color c2, int w, int h)
        {
            Point p1 = new Point();
            Point p2 = new Point(w, h);
            if (horiz)
                p2 = new Point(w, 0);
            else
                p2 = new Point(0, h);
            
            LinearGradientBrush brush = new LinearGradientBrush(p1, p2, c1, c2);
            return brush;
        }

        public Background GetBackground()
        {
            Background bg = null;
            switch (tabControl.SelectedIndex)
            {                
                case 1:
                    bg = new SolidColorBackground() { Color = System.Windows.Media.Color.FromArgb(0, (byte)sR.Value, (byte)sG.Value, (byte)sB.Value) };
                    break;
                case 2:
                    bg = new TransparentColorBackground() { Color = System.Windows.Media.Color.FromArgb(0, (byte)tR.Value, (byte)tG.Value, (byte)tB.Value), Alpha=(byte)tA.Value };
                    break;
                case 3:
                    bg = GetGradientBackground();
                    break;
                case 4:
                    bg = GetImageBackground();
                    break;
            }
            return bg;
        }
        private LinearGradientBackground GetGradientBackground()
        {
            LinearGradientBackground bg = new LinearGradientBackground()
            {
                Color1 = System.Windows.Media.Color.FromArgb(255, (byte)c1R.Value, (byte)c1G.Value, (byte)c1B.Value),
                Color2 = System.Windows.Media.Color.FromArgb(255, (byte)c2R.Value, (byte)c2G.Value, (byte)c2B.Value)
            };
            if (radHor.Checked)
                bg.Orientation = GradientOrientation.Horizontal;
            else
                bg.Orientation = GradientOrientation.Vertical;
            return bg;
        }
        private ImageBackground GetImageBackground()
        {
            ImageBackground res = new ImageBackground();
            res.ImagePath = txtPath.Text;
            res.Repeat = (ImageRepeat)Enum.Parse(typeof(ImageRepeat), (string)cbRepeat.Items[cbRepeat.SelectedIndex]);
            res.PositionX = (PositionX)Enum.Parse(typeof(PositionX), (string)cbPosX.Items[cbPosX.SelectedIndex]);
            res.PositionY = (PositionY)Enum.Parse(typeof(PositionY), (string)cbPosY.Items[cbPosY.SelectedIndex]);
            return res;
        }
    }
}
